package store.swing.product;

import store.domain.Product;
import store.util.AppUtils;

import javax.swing.table.AbstractTableModel;
import java.util.List;

public class ProductTableModel extends AbstractTableModel {
    private static final String[] COLUMN_NAMES = {"ID", "Артикул", "Название", "Цена", "Количество"};
    private static List<Product> products = AppUtils.getService("productService").readAll();


    @Override
    public int getRowCount() {
        return products.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override
    public String getColumnName(int column) {
        return COLUMN_NAMES[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return products.get(rowIndex).getProductId().intValue();
            case 1:
                return products.get(rowIndex).getProductEan();
            case 2:
                return products.get(rowIndex).getNameRuRu();
            case 3:
                return products.get(rowIndex).getProductPrice().intValue();
            case 4:
                return products.get(rowIndex).getProductQuantity().intValue();
        }
        return null;
    }

    public static List<Product> getProducts() {
        return products;
    }

    public static void setProducts(List<Product> products) {
        ProductTableModel.products = products;
    }
}
